package com.railway.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.railway.bean.Basic;
import com.railway.bean.BasicDetailed;
import com.railway.bean.ImagesDetailed;
import com.railway.bean.Province;
import com.railway.bean.Station;
import com.railway.bean.Title;
import com.railway.dao.BasicDao;
import com.railway.dao.impl.BasicDaoImpl;
import com.railway.service.BasicService;


public class BasicServiceImpl implements BasicService {

	BasicDao bd = new BasicDaoImpl();
	@Override
	public List<Basic> selectBasic(Map map) {
		Set<String> keys = map.keySet(); //取出条件中所有的键
		List<Basic> li;//创建一个List<Basic>类型的集合
		for (String key : keys) {
			if("service_id".equals(key)) {
				int service_id = Integer.parseInt(((String[])map.get(key))[0]);//取出service_id
				 li = bd.selectBasic(service_id);//通过BasicDao的selectBasic()方法给li赋值
				 //取出li中的Basic并通过BasicDao查询BasicDetailed并用li2集合封装
				 List<BasicDetailed> li2 = bd.selectBasicDetailed(li.get(0).getService_id());
				 li2 = addTitle(li2);//给li2 中的各个BasicDetailed 添加标题
				 for (String key1 : keys) {
					if("province_id".equals(key1) && !"0".equals(((String[])map.get(key1))[0])) {
						li2 = addProvince(li2);//给li2 中的各个BasicDetailed 添加省
					}
					if("images_id".equals(key1) && !"0".equals(((String[])map.get(key1))[0])) {
						li2 = addImages(li2);//给li2 中的各个BasicDetailed 添加图片
					}
				}
				li.get(0).setBasicDetailed(li2);
				return li;
			}
		}
		return null;
	}

	public List<BasicDetailed> addTitle(List<BasicDetailed> li2) {
		for (BasicDetailed li : li2) {
			li.setTitle(bd.selectTitle(li.getTitle_id()).get(0));
		}
		return li2;
	}
	
	public List<BasicDetailed> addProvince(List<BasicDetailed> li2) {
		for (BasicDetailed li : li2) {
			Province pro = bd.selectProvince(li.getProvince_id()).get(0);
			pro.setStation(addStation(pro.getStation_id()));
			pro.setImagesDetailed(addImagesDetailed(pro.getImagesDetailed_id()));
			li.setProvince(pro);
		}
		return li2;
	}
	
	
	/**
	 * 添加站
	 * @param station_id
	 * @return
	 * List<Station>
	 */
	public List<Station> addStation(int station_id) {
		List<Station> station= bd.selectStation(station_id);
		return station;
	}
	
	
	/**
	 * 添加图片的细节
	 * @param imagesdetailed_id
	 * @return
	 * List<ImagesDetailed>
	 */
	public List<ImagesDetailed> addImagesDetailed(int imagesdetailed_id) {
		List<ImagesDetailed> imagesDetailed= bd.selectImagesDetailed(imagesdetailed_id);
		return imagesDetailed;
	}
	
	/**
	 * 添加BasicDetailed图片
	 * @param li2
	 * @return
	 * List<BasicDetailed>
	 */
	public List<BasicDetailed> addImages(List<BasicDetailed> li2) {
		for (BasicDetailed li : li2) {
			li.setImages((bd.selectImages(li.getImages_id()).get(0)));
		}
		return li2;
	}
	  
}
